package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/21 11:26
 * @description
 */
public class Code112 {
    private List<Integer> list = new ArrayList<>();
    private boolean flag = false;

    public boolean hasPathSum(TreeNode root, int sum) {
        setList(root, sum, 0);
        return flag;
    }

    private void setList(TreeNode node, int sum, int index) {
        if (node != null) {
            list.add(index, node.val);
            index++;
            if (node.left == null && node.right == null) {
                int t = 0;
                for (int i = 0; i < index; i++) {
                    t += list.get(i);
                }
                if (t == sum) {
                    flag = true;
                }
            }
            setList(node.left, sum, index);
            setList(node.right, sum, index);
        }
    }
}
